package com.kawaiior.asoulworld.item.weapon;

import com.kawaiior.asoulworld.item.ItemASWeapon;
import com.kawaiior.asoulworld.item.ModItems;
import com.kawaiior.asoulworld.potion.ModPotions;
import com.kawaiior.asoulworld.util.AsoulWorldUtils;
import com.kawaiior.asoulworld.util.EntityUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.EntityEquipmentSlot;
import net.minecraft.item.EnumAction;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;

public class ItemKiraWand extends ItemASWeapon {
    public ItemKiraWand(String name, ToolMaterial material) {
        super(name, material);
        useable = true;
        base_cd = 10f;
        attackDamage = 5f;
        base_power = 10f;
    }

    @Override
    public int getMaxDamage(ItemStack stack) {
        return 0;
    }

    @Override
    public EnumAction getItemUseAction(ItemStack stack)
    {
        return EnumAction.NONE;
    }

    @Override
    public int getMaxItemUseDuration(ItemStack stack)
    {
        return 5;
    }

    @Nullable
    private Entity getPlayerLookTarget(World world, EntityLivingBase living) {
        Entity pointedEntity = null;
        double range = 128D;
        Vec3d srcVec = new Vec3d(living.posX, living.posY + living.getEyeHeight(), living.posZ);
        Vec3d lookVec = living.getLook(1.0F);
        Vec3d destVec = srcVec.addVector(lookVec.x*range,lookVec.y*range,lookVec.z*range);
        float var9 = 1.0F;
        List<Entity> possibleList = world.getEntitiesWithinAABBExcludingEntity(living, living.getEntityBoundingBox().expand(lookVec.x * range, lookVec.y * range, lookVec.z * range).grow(var9, var9, var9));
        double hitDist = 0;

        for (Entity possibleEntity : possibleList) {


            if (possibleEntity.canBeCollidedWith()) {
                float borderSize = possibleEntity.getCollisionBorderSize();
                AxisAlignedBB collisionBB = possibleEntity.getEntityBoundingBox().grow((double) borderSize, (double) borderSize, (double) borderSize);
                RayTraceResult interceptPos = collisionBB.calculateIntercept(srcVec, destVec);

                if (collisionBB.contains(srcVec)) {
                    if (0.0D < hitDist || hitDist == 0.0D) {
                        pointedEntity = possibleEntity;
                        hitDist = 0.0D;
                    }
                } else if (interceptPos != null) {
                    double possibleDist = srcVec.distanceTo(interceptPos.hitVec);

                    if (possibleDist < hitDist || hitDist == 0.0D) {
                        pointedEntity = possibleEntity;
                        hitDist = possibleDist;
                    }
                }
            }
        }
        return pointedEntity;
    }

    @Nonnull
    @Override
    public ActionResult<ItemStack> onItemRightClick(World world, EntityPlayer player, @Nonnull EnumHand hand) {
        player.setActiveHand(hand);
        ItemStack stack = player.getHeldItem(hand);
        if (!useable || world.isRemote)
        {return ActionResult.newResult(EnumActionResult.FAIL, stack);}
        Entity pointedEntity = getPlayerLookTarget(world, player);
        if (pointedEntity instanceof EntityPlayer)
        {
            if (player.getHeldItemOffhand().getItem() == ModItems.DIANA_CANDY){
                pointedEntity.attackEntityFrom(DamageSource.causePlayerDamage(player).setMagicDamage().setDamageBypassesArmor(), 10f);
                pointedEntity.world.createExplosion(player, pointedEntity.posX, pointedEntity.posY + 2.0D, pointedEntity.posZ, 2, false);
                if (!(((EntityPlayer) pointedEntity).getItemStackFromSlot(EntityEquipmentSlot.CHEST).isEmpty())){
                    ((EntityPlayer) pointedEntity).getItemStackFromSlot(EntityEquipmentSlot.CHEST).damageItem(100, (EntityLivingBase) pointedEntity);
                }
                player.getCooldownTracker().setCooldown(this, 5);
                return ActionResult.newResult(EnumActionResult.SUCCESS, stack);
            }
            ((EntityPlayer) pointedEntity).heal(10f);
            player.getCooldownTracker().setCooldown(this, getCoolDownTicks(stack));
        }
        else if (pointedEntity instanceof EntityLiving)
        {
            if (AsoulWorldUtils.consumeInventoryItem(player, s -> s.getItem() == ModItems.DIANA_CANDY, 1))
            {
                pointedEntity.attackEntityFrom(DamageSource.causePlayerDamage(player).setMagicDamage().setDamageBypassesArmor(),30f);
                pointedEntity.world.createExplosion(player,pointedEntity.posX,pointedEntity.posY+2.0D,pointedEntity.posZ,3,false);
                player.getCooldownTracker().setCooldown(this, 5);
            }
            else {
                pointedEntity.attackEntityFrom(DamageSource.causePlayerDamage(player).setMagicDamage().setDamageBypassesArmor(), 10f);
                pointedEntity.world.createExplosion(player, pointedEntity.posX, pointedEntity.posY + 2.0D, pointedEntity.posZ, 2, false);
                player.getCooldownTracker().setCooldown(this, 20);
            }
        }
        else
        {
            if (AsoulWorldUtils.consumeInventoryItem(player, s -> s.getItem() == ModItems.DIANA_CANDY, 1))
            {
                player.getCooldownTracker().setCooldown(this, 15);
            }
            else {
                player.getCooldownTracker().setCooldown(this, getCoolDownTicks(stack));
            }
        }
        return ActionResult.newResult(EnumActionResult.SUCCESS, stack);
    }

    @Override
    public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)
    {

    }


    //    @Override
//    public void onPlayerStoppedUsing(ItemStack stack, World worldIn, EntityLivingBase entityLiving, int timeLeft)
//    {
//        if (!useable)
//        {
//            return;
//        }
//
//        if (!worldIn.isRemote)
//        {
//            if (entityLiving instanceof EntityPlayer)
//            {
//                Entity pointedEntity = getPlayerLookTarget(worldIn, entityLiving);
//                EntityPlayer entityplayer = (EntityPlayer)entityLiving;
//                Vec3d lookVec = entityplayer.getLook(1.0F);
//                stack.damageItem(16, entityLiving);
//                if (pointedEntity == null)
//                {
//                    if (AsoulWorldUtils.consumeInventoryItem(entityplayer, s -> s.getItem() == ModItems.DIANA_CANDY, 1))
//                    {
//                        entityplayer.getCooldownTracker().setCooldown(this, 20);
//                        return;
//                    }
//                    entityplayer.getCooldownTracker().setCooldown(this, getCoolDownTicks(stack));
//                }
//                else if (pointedEntity instanceof EntityPlayer)
//                {
//                    ((EntityPlayer) pointedEntity).heal(10f);
//                    entityplayer.getCooldownTracker().setCooldown(this, getCoolDownTicks(stack));
//                }
//                else if (pointedEntity instanceof EntityLiving)
//                {
//                    if (AsoulWorldUtils.consumeInventoryItem(entityplayer, s -> s.getItem() == ModItems.DIANA_CANDY, 1))
//                    {
//                        pointedEntity.attackEntityFrom(DamageSource.causePlayerDamage(entityplayer).setMagicDamage().setDamageBypassesArmor(),30f);
//                        pointedEntity.world.createExplosion(entityplayer,pointedEntity.posX,pointedEntity.posY+2.0D,pointedEntity.posZ,2,true);
//                        entityplayer.getCooldownTracker().setCooldown(this, 5);
//                        return;
//                    }
//                    pointedEntity.attackEntityFrom(DamageSource.causePlayerDamage(entityplayer).setMagicDamage().setDamageBypassesArmor(),10f);
//                    pointedEntity.world.createExplosion(entityplayer,pointedEntity.posX,pointedEntity.posY+2.0D,pointedEntity.posZ,1,false);
//                    entityplayer.getCooldownTracker().setCooldown(this, 20);
//                }
//            }
//        }
//
//    }

}
